# Given a telephone number, find all the permutations of the letters assuming 1=abc, 2=def, etc.
# 1 => a,b,c
# 2 => d,e,f
# 3 => g,h,i
# 4 => j,k,l
# 5 => m,n,o
# 6 => p,q,r
# 7 => s,t,u
# 8 => v,w,x
# 9 => y,z

char_group = ('a'..'z').each_slice(3).to_a
#p char_group.slice(0,:all)
# input : 123

def get_permutation_string(groups,str)
  return groups[0].map{|char| "#{str}#{char}"} if groups.length == 1
  groups[0].map {|char| get_permutation_string(groups.slice(1,:all),"#{str}#{char}")}.flatten
end

def dial(numbers)
  char_group = ('a'..'z').each_slice(3).to_a
  groups = numbers.chars.inject([]){|result,n| n == '0' ? result : result << char_group[n.to_i-1]}
  return get_permutation_string(groups,'') unless groups.empty?
end

p dial("1012")


# Print a singly-linked list backwards, in constant space and linear time.

def reverse_print(list)
  "#{reverse_print(list.slice(1,:all))}#{list[0]}" unless list.empty?
end
p reverse_print([1,2,3])
